package com.cndw.kongfu.messagepush;

import cn.jpush.api.ErrorCodeEnum;
import cn.jpush.api.JPushClient;
import cn.jpush.api.MessageResult;

public class MessagePush {
	
	private static String masterSecret = "3b802d4341c892a437ac30c6";
	private static String appKey = "43dc9949baa023129e250862";
	
	
	public static final int MAX = Integer.MAX_VALUE;
	public static final int MIN = MAX/2;
	
	public static void pushMessage(String msg) {
		if (msg == null || msg.length() <= 0) {
			return;
		}
		JPushClient jpush = new JPushClient(masterSecret, appKey);
		
		// 对android和ios设备发送,同时指定离线消息保存时间
		//JPushClient jpush = new JPushClient(masterSecret, appKey, timeToLive);
		
		// 指定某种设备发送
		//JPushClient jpush = new JPushClient(masterSecret, appKey, DeviceEnum.Android);
		
		// 指定某种设备发送，并且指定离线消息保存时间
		//JPushClient jpush = new JPushClient(masterSecret, appKey, timeToLive, DeviceEnum.IOS);
		
		int sendNo = getRandomSendNo();
		String msgTitle = "从 MessagePushClient发送";
		 
		MessageResult msgResult = jpush.sendNotificationWithAppKey(sendNo, msgTitle, msg);
		if (null != msgResult) {
		    if (msgResult.getErrcode() == ErrorCodeEnum.NOERROR.value()) {
		        System.out.println("发送成功， sendNo=" + msgResult.getSendno());
		    } else {
		        System.out.println("发送失败， 错误代码=" + msgResult.getErrcode() + ", 错误消息=" + msgResult.getErrmsg());
		    }
		} else {
		    System.out.println("无法获取数据");
		}
	}
	
	/**
	 * 保持 sendNo 的唯一性是有必要的
	 * It is very important to keep sendNo unique.
	 * @return sendNo
	 */
	public static int getRandomSendNo() {
	    return (int) (MIN + Math.random() * (MAX - MIN));
	}

}
